package com.example.android_entrainement;


import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jim on 20/07/13.
 */
public class CSVtoCardDb {


    public CSVtoCardDb()
    {};

     private List<String[]> ReadCSVwithCSVReader (InputStreamReader is)
    {
        String next[] = {};
        List<String[]> list = new ArrayList<String[]>();
        String data = "";
        try {
            CSVReader reader = new CSVReader(is);
            for(;;) {
                next = reader.readNext();


                if(next != null) {
                    list.add(next);

                }
                else {
                    break;
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }

    public ArrayList<CardEntity> GetListCardsFromCSV (InputStreamReader is)
    {
        ArrayList<CardEntity> toReturn = new ArrayList<CardEntity>();
        for(final String[] str:ReadCSVwithCSVReader(is))
        {
           toReturn.add(new CardEntity(str[0],str[1],str[2],str[3]));
        }
        return toReturn;
    }



    /*public void FillDBwithCSVReaderResult (SQLiteDatabase db,List<String[]> CardsListResult)
    {
        String tableName = Card_Table.CardEntry.TABLE_NAME;


        String columns = Card_Table.CardEntry.COLUMN_NAME_CARDS_NAME+","
                + Card_Table.CardEntry.COLUMN_NAME_IMG__ID;//+","
               // + Card_Table.CardEntry.COLUMN_NAME_CARDS_IMAGE;


        String InsertString1 = "INSERT INTO " + tableName + " (" + columns + ") values(";
        String InsertString2 = ");";

        db.beginTransaction();
        int itemIndex = 0;
        while (!CardsListResult.isEmpty()) {
            StringBuilder sb = new StringBuilder(InsertString1);
            String[] strTab = CardsListResult.get(1);
            String test = strTab[1];
            sb.append("'" + CardsListResult.get(itemIndex)[0]  + "','"); //Nom
            sb.append(CardsListResult.get(itemIndex)[1] + "'"); // Img__ID
            //sb.append(CardsListResult.get(itemIndex)[2] + "'"); // Image
            sb.append(InsertString2);
            db.execSQL(sb.toString());
            CardsListResult.remove(itemIndex);
        }

        db.setTransactionSuccessful();
        db.endTransaction();
    }*/

}
